﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Antioch.Base
{
    public class Weapon
    {
        public String InventorySlot { get; protected set; }
        public String AmunitionSlot { get; protected set; }
        public Texture2D HudIcon { get; protected set; }
        public String HitWorldEffect { get; protected set; }
        public String HitFleshEffect { get; protected set; }
        
        public virtual void Update(Jemgine.Data.EntityRecord Holder, bool FirePressed, Vector2 Aim) { }
        public virtual void Draw(Jemgine.Data.EntityRecord Holder, PlayerStates.StateMachine Machine,
            Jemgine.Renderer.RenderContext Context) { }
    }

    internal class Pistol : Weapon
    {
        Texture2D _crosshair;
        Random _random = new Random();

        internal Pistol(ContentManager Content)
        {
            InventorySlot = "Pistol";
            AmunitionSlot = "PistolAmmo";
            HudIcon = Content.Load<Texture2D>("demo/pistol");
            HitWorldEffect = "pistol/hit_world";
            HitFleshEffect = "pistol/hit_flesh";
            _crosshair = Content.Load<Texture2D>("pistol/crosshair");
            
        }

        int FireTimer = 0;

        public override void Update(Jemgine.Data.EntityRecord Holder, bool FirePressed, Vector2 Aim)
        {
            if (FirePressed)
            {
                FireTimer -= 1;
                if (FireTimer < 0)
                {
                    FireTimer = 3;
                    var Module = Holder.Sim.GetModule<BulletModule>();
                    if (Module == null) return;

                    Matrix AimJitter = Matrix.CreateRotationZ((0.2f * (float)_random.NextDouble()) - 0.1f);
                    Aim = Vector2.Transform(Aim, AimJitter);

                    Module.FireBullet(Holder.Position, Aim, 80.0f, BulletModule.BulletTarget.Enemy, this);
                }
            }
            else
                FireTimer = 0;
            
        }

        public override void Draw(Jemgine.Data.EntityRecord Holder, PlayerStates.StateMachine State,
            Jemgine.Renderer.RenderContext Context)
        {
            Context.PushCleanMatrix(Matrix.Identity);
            Context.DrawOrientedSprite(_crosshair, State.Input.MouseAim, Vector2.One);
            Context.PopMatrix();
        }
    }
}
